package com.kaishengit.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.kaishengit.dao.MessageDao;
import com.kaishengit.pojo.Message;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.Trend;
import com.kaishengit.pojo.User;
import com.kaishengit.service.LinkRecordService;
import com.kaishengit.service.LinkmanService;
import com.kaishengit.service.TrendService;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.Util;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements SessionAware ,ApplicationAware{

	private static final long serialVersionUID = 1L;
	private MessageDao messageDao;
	private LinkmanService linkmanService;
	private LinkRecordService linkRecordService;
	private TrendService trendService;
	public static final String LIST = "list";

	private Map<String, Object> session;

	public void savemsage(String content) {
		Message message = new Message();
		message.setCreatetime(DateUtil.getDate());
		message.setContent(content);
		message.setUser((User) getFromSession("user"));
		message.setProject((Project) getFromSession("project"));
		messageDao.save(message);
	}

	public void sendJson(Object o) {
		Gson gson = new Gson();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(gson.toJson(o));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	public Trend trends(String content) {
		Trend trend = new Trend();
		trend.setContent(content);
		trend.setDate(Util.getDateTiem());
		trend.setUser((User) getFromSession("user"));
		trend.setProject((Project) getFromSession("project"));
		trendService.save(trend);
		return trend;
	}

	public void putInSession(String key, Object value) {
		session.put(key, value);
	}
	
	
	@Override
	public String execute() throws Exception {
		return list();
	}

	public abstract String list() throws Exception;

	

	public Object getFromSession(String key) {
		return session.get(key);
	}
	public String getMethod() {
		return ServletActionContext.getRequest().getMethod();
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	@Autowired
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public LinkmanService getLinkmanService() {
		return linkmanService;
	}

	@Autowired
	public void setLinkmanService(LinkmanService linkmanService) {
		this.linkmanService = linkmanService;
	}

	public LinkRecordService getLinkRecordService() {
		return linkRecordService;
	}

	@Autowired
	public void setLinkRecordService(LinkRecordService linkRecordService) {
		this.linkRecordService = linkRecordService;
	}
	
	
	public TrendService getTrendService() {
		return trendService;
	}

	@Autowired
	public void setTrendService(TrendService trendService) {
		this.trendService = trendService;
	}
	
}
